package com.josework.springhelloworld.service.impl;

import com.josework.springhelloworld.mapper.ArticleMapper;
import com.josework.springhelloworld.model.Article;
import com.josework.springhelloworld.service.ArticleService;
import com.josework.springhelloworld.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public int saveArticle(Article article) {
        if (article.getAddTime() == null){
            article.setAddTime(DateUtils.UTCDATE());
        }
        return articleMapper.insert(article);
    }

    @Override
    public int deleteArticle(Article article) {
        return articleMapper.deleteById(article.getId());
    }

    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateById(article);
    }

    @Override
    public Article getArticle(Long id) {
        return articleMapper.selectById(id);
    }

    @Override
    public List<Article> getAll() {
//        QueryWrapper wrapper  = new QueryWrapper<>();
//        wrapper.eq("author", "JosephXu1");
        return articleMapper.selectList(null);
    }
}
